//
//  UploadIdCardViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 上传身份证

#import "UploadIdCardViewController.h"
#import "TZImagePickerController.h"

@interface UploadIdCardViewController ()<TZImagePickerControllerDelegate>
/**
 titleLab
 */
@property (nonatomic , strong) UILabel *titleLab;

/**
 正面背景框
 */
@property (nonatomic , strong) UIImageView *frontIDCardBg;
/**
 正面身份证图片
 */
@property (nonatomic , strong) UIImageView *uploadfrontImgView;

/**
 反面背景框
 */
@property (nonatomic , strong) UIImageView *reverseSideIDCardBg;
/**
 反面身份证图片
 */
@property (nonatomic , strong) UIImageView *uploadReverseSideImgView;

/**
 uploadBtn
 */
@property (nonatomic , strong) UIButton *uploadBtn;

/**
 frontOrReverseStatus
 */
@property (nonatomic , assign) BOOL isFrontPhoto;
/**
 photosArr
 */
@property (nonatomic , strong) NSMutableArray *photosArr;

@end

@implementation UploadIdCardViewController


#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    
    self.view.backgroundColor = KWhiteColor;
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.frontIDCardBg];
    [self.frontIDCardBg addSubview:self.uploadfrontImgView];
    [self.view addSubview:self.reverseSideIDCardBg];
    [self.reverseSideIDCardBg addSubview:self.uploadReverseSideImgView];
    [self.view addSubview:self.uploadBtn];
}

- (void) initLayout {
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(26 + TJ_NavBarHeight);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(30);
    }];
    [self.frontIDCardBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(50);
        make.size.mas_equalTo(CGSizeMake(200 * [JTool getScreenWidthScale], 136 * [JTool getScreenWidthScale]));
    }];
    [self.uploadfrontImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.frontIDCardBg.mas_centerX);
        make.centerY.mas_equalTo(self.frontIDCardBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150 * [JTool getScreenWidthScale], 100 * [JTool getScreenWidthScale]));
    }];
    [self.reverseSideIDCardBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.frontIDCardBg.mas_bottom).offset(60);
        make.size.mas_equalTo(CGSizeMake(200 * [JTool getScreenWidthScale], 136 * [JTool getScreenWidthScale]));
    }];
    [self.uploadReverseSideImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.reverseSideIDCardBg.mas_centerX);
        make.centerY.mas_equalTo(self.reverseSideIDCardBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150 * [JTool getScreenWidthScale], 100 * [JTool getScreenWidthScale]));
    }];
    [self.uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.reverseSideIDCardBg.mas_bottom).offset(60);
        make.right.mas_equalTo(-30);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(48 * [JTool getScreenWidthScale]);
    }];
}

- (void) initAction {
    
    UITapGestureRecognizer *frontTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frontTapped)];
    [self.frontIDCardBg addGestureRecognizer:frontTap];
    
    UITapGestureRecognizer *reverseSideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reverseSideTapped)];
    [self.reverseSideIDCardBg addGestureRecognizer:reverseSideTap];
    
    [self.uploadBtn addTarget:self action:@selector(uploadIDCardTapped) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initData {
    
}

#pragma mark - delegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    
    [self.photosArr addObject:photos[0]];
    
    if (self.isFrontPhoto) {
        self.uploadfrontImgView.image = photos[0];
    }else {
        self.uploadReverseSideImgView.image = photos[0];
    }
}
#pragma mark - target action
- (void)frontTapped {
    @weakify(self);
    [[AlertAction sharedInstance] showAlertSheetWithTitle:nil message:nil itemsArr:@[@"拍照",@"相册"] selectItemBlock:^(NSString *selectItem) {
        @strongify(self);
        self.isFrontPhoto = YES;
        if ([selectItem isEqualToString:@"拍照"]) {
            [self takePhoto];
        }else if ([selectItem isEqualToString:@"相册"]) {
            [self photoAlbum];
        }
    }];
}
- (void)reverseSideTapped {
    @weakify(self);
    [[AlertAction sharedInstance] showAlertSheetWithTitle:nil message:nil itemsArr:@[@"拍照",@"相册"] selectItemBlock:^(NSString *selectItem) {
        @strongify(self);
        self.isFrontPhoto = NO;
        if ([selectItem isEqualToString:@"拍照"]) {
            [self takePhoto];
        }else if ([selectItem isEqualToString:@"相册"]) {
            [self photoAlbum];
        }
    }];
}

- (void)uploadIDCardTapped {
    //上传图片接口请求
   
    NSString *account = [UserInfoDataProvider sharedInstance].userInfo.phone;
    [[[PigMarketApi sharedInstance] uploadPhotosWithPhotosArr:self.photosArr account:account type:UpDateImageType_Header] subscribeNext:^(PhotosList * _Nullable uploadPhotosList) {
        
        
    } error:^(NSError * _Nullable error) {
        
    }];
    
}
#pragma mark - private method

/** 拍照 */
- (void)takePhoto {
    
}
/** 相册 */
- (void)photoAlbum {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
    imagePickerVc.allowTakeVideo = NO;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
#pragma mark - getter and setter
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont xxLargerFont];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"请上传您本人身份证正反面照片";
    }
    return _titleLab;
}
- (UIImageView *)frontIDCardBg {
    if (!_frontIDCardBg) {
        _frontIDCardBg = [[UIImageView alloc] init];
        _frontIDCardBg.image = [UIImage imageNamed:@"user_realname_uploadIDbg"];
        _frontIDCardBg.userInteractionEnabled = YES;
    }
    return _frontIDCardBg;
}
- (UIImageView *)uploadfrontImgView {
    if (!_uploadfrontImgView) {
        _uploadfrontImgView = [[UIImageView alloc] init];
        _uploadfrontImgView.image = [UIImage imageNamed:@"user_realname_uploadID"];
    }
    return _uploadfrontImgView;
}

- (UIImageView *)reverseSideIDCardBg {
    if (!_reverseSideIDCardBg) {
        _reverseSideIDCardBg = [[UIImageView alloc] init];
        _reverseSideIDCardBg.image = [UIImage imageNamed:@"user_realname_uploadIDbg"];
        _reverseSideIDCardBg.userInteractionEnabled = YES;
    }
    return _reverseSideIDCardBg;
}
- (UIImageView *)uploadReverseSideImgView {
    if (!_uploadReverseSideImgView) {
        _uploadReverseSideImgView = [[UIImageView alloc] init];
        _uploadReverseSideImgView.image = [UIImage imageNamed:@"user_realname_uploadreversesideID"];

    }
    return _uploadReverseSideImgView;
}
- (UIButton *)uploadBtn {
    if (!_uploadBtn) {
        _uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_uploadBtn setTitle:@"上传" forState:UIControlStateNormal];
        [_uploadBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    }
    return _uploadBtn;
}
- (NSMutableArray *)photosArr {
    if (!_photosArr) {
        _photosArr = [[NSMutableArray alloc] init];
    }
    return _photosArr;
}
@end
